﻿using MvcWeb.Enums;
using MiniExcelLibs;

namespace MvcWeb.Data
{
    public class DataSeed
    {
        public static void Seed(IWebHostEnvironment webHostEnvironment)
        {
            string path = webHostEnvironment.WebRootPath + @"/excel/浙商基金信息技术部开发工程师作业.xlsx";
            //读取excel数据
            var datas = MiniExcel.Query(path, sheetName: "数据");
            //获取表头
            var headed = (IDictionary<string, object>)datas.First();

            //映射列对应的列名
            Dictionary<Column, string> stockNames = new Dictionary<Column, string>();
            foreach (Column c in Enum.GetValues(typeof(Column)))
            {
                var str = headed[c.ToString()].ToString() ?? "";
                if (str.Length > 8)
                {
                    //去掉股票代码
                    var stockName = str.Substring(0, str.Length - 8);
                    GlobalVar.stocksData[stockName] = new List<double>();
                    stockNames[c] = stockName;
                }
            }

            GlobalVar.stocksData["上证指数"] = new List<double>();
            //从第二行开始遍历Excel行
            foreach (var data in datas.Skip(1))
            {
                GlobalVar.dateTimes.Add(data.A);
                GlobalVar.stocksData["上证指数"].Add(data.B);

                var temp = (IDictionary<string, object>)data;
                //遍历股票价格数据
                foreach (Column c in Enum.GetValues(typeof(Column)))
                {
                    if (temp[c.ToString()] != null)
                    {
                        GlobalVar.stocksData[stockNames[c]].Add((double)temp[c.ToString()]);
                    } else
                    {
                        if (GlobalVar.stocksData[stockNames[c]].Count() > 0)
                        {
                            GlobalVar.stocksData[stockNames[c]].Add(GlobalVar.stocksData[stockNames[c]].Last());                            
                        }

                    }
                }

            }

        }
    }
}
